package com.example.project.base;

import com.example.project.config.shiro.ShiroCacheObj;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description: 接口控制层基础类
 *
 */
public class BaseApi {
    protected static Logger logger = LoggerFactory.getLogger(BaseApi.class);

    /**
     * 获取当前登录的信息
     *
     * @return AuthInfo
     */
    protected ShiroCacheObj currAdminInfo() {
        return (ShiroCacheObj) SecurityUtils.getSubject().getPrincipal();
    }

    protected ResultBean ok() {
        return ok(ConstKit.SUCCESS_MSG);
    }

    protected ResultBean ok(String msg) {
        return ok(msg, null);
    }

    protected ResultBean ok(Object object) {
        return ok(ConstKit.SUCCESS_MSG, object);
    }

    protected ResultBean ok(String msg, Object object) {
        if (object instanceof ArrayList) {
            return new ResultBean(ResultBean.SUCCESS, msg, new PageInfo<>((List<? extends Object>) object));
        }
        return new ResultBean(ResultBean.SUCCESS, msg, object);
    }

    protected ResultBean fail() {
        return fail(ConstKit.FAIL_MSG);
    }

    protected ResultBean fail(String msg) {
        return fail(msg, null);
    }

    protected ResultBean fail(Object object) {
        return fail(ConstKit.FAIL_MSG, object);
    }

    protected ResultBean fail(String msg, Object object) {
        return new ResultBean(ResultBean.FAIL, msg, object);
    }


}
